package com.jessechanrui.template.common.template.auth.infrastructure.support.repositoryImpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jessechanrui.template.common.template.auth.domain.User;
import com.jessechanrui.template.common.template.auth.domain.support.repository.UserRepository;
import com.jessechanrui.template.common.template.auth.infrastructure.converter.UserConverter;
import com.jessechanrui.template.common.template.auth.infrastructure.mapper.UserMapper;
import com.jessechanrui.template.common.template.auth.infrastructure.po.UserPO;
import com.jessechanrui.template.core.entity.BasePageRequest;
import com.jessechanrui.template.core.exception.DataBaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenrui
 * @version 1.0
 * @description UserRepositoryImpl
 * @date 2025/8/8
 */
@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserConverter userConverter;

    @Override
    public User findByUserName(String userName) {
        UserPO userPO = new LambdaQueryChainWrapper<>(userMapper)
                .eq(UserPO::getUserName, userName)
                .one();
        return userConverter.toEntity(userPO);
    }

    @Override
    public User findByGerritUserName(String gerritUserName) {
        UserPO userPO = new LambdaQueryChainWrapper<>(userMapper)
                .eq(UserPO::getGerritUserName, gerritUserName)
                .one();
        return userConverter.toEntity(userPO);
    }

    @Override
    public List<User> list() {
        return userConverter.toEntityList(userMapper.selectList(null));
    }

    @Override
    public IPage<User> listByPage(BasePageRequest request) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        String keyword = request.getKeyword();
        //关键字查询
        if (StrUtil.isNotBlank(keyword)) {
            queryWrapper.and(w -> w.like(UserPO::getUserName, keyword).or().like(UserPO::getUserNickname, keyword));
        }
        Page<UserPO> page = new Page<>(request.getPageNum(), request.getPageSize());
        return userConverter.convertPage(userMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void addUser(User user) {
        int status = userMapper.insert(userConverter.toPO(user));
        if(status == 0){
            throw new DataBaseException("添加用户失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void addUsers(List<User> users) {
        List<UserPO> poList = userConverter.toPOList(users);
        List<List<UserPO>> partition = Lists.partition(poList, 10);
        partition.forEach(pos -> userMapper.insert(pos));
    }

    @Override
    public void delete(Integer id) {
        int status = userMapper.deleteById(id);
        if(status == 0){
            throw new DataBaseException("删除用户失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteByUserIds(List<String> userIds){
        List<List<String>> partition = Lists.partition(userIds, 10);
        partition.forEach(ids -> {
            if(CollectionUtil.isNotEmpty(ids)){
                LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(UserPO::getUserId,ids);
                userMapper.delete(queryWrapper);
            }
        });
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateUsers(List<User> users) {
        List<UserPO> poList = userConverter.toPOList(users);
        //分批更新
        List<List<UserPO>> partition = Lists.partition(poList, 10);
        partition.forEach(pos -> userMapper.updateById(pos));
    }

    @Override
    public void updateUser(User user) {
        int status = userMapper.updateById(userConverter.toPO(user));
        if(status == 0){
            throw new DataBaseException("更新用户失败");
        }
    }
}

